package com.shelpe.services.sinbadgateway.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbadgateway.client.SimbaProxyClient;
import com.shelpe.services.sinbadgateway.client.SimbaProxyClient.InsightWordData;
import com.shelpe.services.sinbadgateway.controller.AutoProductQueueController.AutoDrive;
import com.shelpe.services.sinbadgateway.entity.jpa.Product;
import com.shelpe.services.sinbadgateway.repository.jpa.ProductRepository;

@Service
public class ProductService {

	private final static String[] TEMPLATE = new String[0];

	@Autowired
	private ProductRepository productRepository;
	@Autowired
	private SimbaProxyClient simbaProxyClient;

	public void fillupAutoProduct(long userID, AutoDrive dto) {
		List<Product> products = this.productRepository.findByUserID(userID);

		List<String> categoryIDs = new ArrayList<String>();
		for (Product prod : products) {
			AutoDrive.AutoDriveItem item = new AutoDrive.AutoDriveItem();
			item.setProductID(prod.getProductID());
			String name = prod.getProductName();
			if (prod.getProductName() != null && prod.getProductName().length() > 20) {
				name = prod.getProductName().substring(0, 20);
			}
			item.setFirstCreativeName(name);
			item.setFirstCreativeUrl(prod.getPictureUrl());
			if (prod.getItemImgs() != null) {
				item.setSecondCreativeName(prod.getProductName() != null && prod.getProductName().length() > 20 ? prod
						.getProductName().substring(prod.getProductName().length() - 20, prod.getProductName().length())
						: prod.getProductName());
				item.setSecondCreativeUrl(prod.getItemImgs().iterator().next());
			}
			if (this.containWord(prod.getProductName())) {
				dto.addItem(item);
				if (prod.getCategoryID() > 0) {
					categoryIDs.add(prod.getCategoryID() + "");
				}
			}
		}

		if (categoryIDs.size() > 0 && dto.getMaxPrice() <= 0) {
			Date yesterday = DateUtils.addDays(DateUtils.truncate(new Date(), Calendar.DATE), -1);
			SimbaProxyClient.CategoryDataResponse res = this.simbaProxyClient.findCategoryData(
					categoryIDs.toArray(TEMPLATE),
					DateFormatUtils.format(DateUtils.addDays(yesterday, -3), "yyyy-MM-dd"),
					DateFormatUtils.format(yesterday, "yyyy-MM-dd"), userID);
			if (res != null && res.getRes() != null && res.getRes().getDatas() != null
					&& res.getRes().getDatas().getWordDatas() != null) {
				for (InsightWordData data : res.getRes().getDatas().getWordDatas()) {
					List<Product> selected = this.filterByCategoryID(products,
							NumberUtils.toLong(data.getCategoryID(), -100));
					for (Product prod : selected) {
						AutoDrive.AutoDriveItem item = this.filterByProductID(dto.getItems(), prod.getProductID());
						if (item != null) {
							item.setMaxPrice(((int) NumberUtils.toFloat(data.getCpc()) * 2));
						}
					}
				}
			}
		}
	}

	private AutoDrive.AutoDriveItem filterByProductID(List<AutoDrive.AutoDriveItem> items, long productID) {
		for (AutoDrive.AutoDriveItem item : items) {
			if (item.getProductID() == productID) {
				return item;
			}
		}

		return null;
	}

	private List<Product> filterByCategoryID(List<Product> products, long categoryID) {
		List<Product> selected = new ArrayList<Product>();
		for (Product prod : products) {
			if (prod.getCategoryID() == categoryID) {
				selected.add(prod);
			}
		}
		return selected;
	}

	private boolean containWord(String productName) {
		return productName != null && !productName.contains("补差") && !productName.contains("补拍")
				&& !productName.contains("邮费") && !productName.contains("最") && !productName.contains("第一")
				&& !productName.contains("减肥") && !productName.contains("丰胸") && !productName.contains("壮阳")
				&& !productName.contains("提高肾阳") && !productName.contains("强阳健体") && !productName.contains("提升性能力")
				&& !productName.contains("增强性欲") && !productName.contains("缩阴") && !productName.contains("增高")
				&& !productName.contains("情趣内衣") && !productName.contains("器具类") && !productName.contains("避孕套")
				&& !productName.contains("医用医疗器械") && !productName.contains("超A") && !productName.contains("高仿")
				&& !productName.contains("1:1") && !productName.contains("一比一") && !productName.contains("水货")
				&& !productName.contains("外贸") && !productName.contains("原单") && !productName.contains("尾单")
				&& !productName.contains("日货") && !productName.contains("日单") && !productName.contains("A货")
				&& !productName.contains("境外货") && !productName.contains("亚太版") && !productName.contains("小货")
				&& !productName.contains("同款") && !productName.contains("治疗") && !productName.contains("治愈")
				&& !productName.contains("止血") && !productName.contains("止痛") && !productName.contains("无效退款")
				&& !productName.contains("无副作用") && !productName.contains("永不反弹") && !productName.contains("立竿见影")
				&& !productName.contains("不复发") && !productName.contains("治愈率") && !productName.contains("稳赚n倍")
				&& !productName.contains("保健品") && !productName.contains("*") && !productName.contains("#")
				&& !productName.contains("※") && !productName.contains("△") && !productName.contains("◇")
				&& !productName.contains("★") && !productName.contains("▲") && !productName.contains("【")
				&& !productName.contains("】") && !productName.contains("『") && !productName.contains("』");
	}
}
